﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using MPR.Poker.DALEntity.SYS;
using Library.AppWeb;

namespace PokerLife.Controllers.SYS
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult User_Load([DataSourceRequest] DataSourceRequest request)
        {
            List<MPR.Poker.Model.SYS.SYS_User> lst = new List<MPR.Poker.Model.SYS.SYS_User>();
            ResultMessage result = new MPR.Poker.DALEntity.SYS.DAL_User().SelectToList(ref lst);
            return Json(lst.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddEdit(int intIDSys=0)
        {
            PokerLife.Models.SYS.UserViewModel model = new PokerLife.Models.SYS.UserViewModel();

            if (intIDSys > 0)
            {
                MPR.Poker.Model.SYS.SYS_User obj = new MPR.Poker.Model.SYS.SYS_User();
                ResultMessage result = new MPR.Poker.DALEntity.SYS.DAL_User().LoadInfo(ref obj, intIDSys);
                if (!result.IsError)
                    model = PokerLife.Models.SYS.UserViewModel.ConvertToModel(obj);
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult AddEdit(PokerLife.Models.SYS.UserViewModel User)
        {
            MPR.Poker.Model.SYS.SYS_User objUser = new MPR.Poker.Model.SYS.SYS_User();
            objUser = PokerLife.Models.SYS.UserViewModel.ConvertTo(User);

            ResultMessage result = new ResultMessage();
            if(objUser.IDSys>0)
                result = new MPR.Poker.DALEntity.SYS.DAL_User().Update(objUser);
            else
                result = new MPR.Poker.DALEntity.SYS.DAL_User().Insert(objUser);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Delete(int intIDSys)
        {
            ResultMessage result = new ResultMessage();
            result = new DAL_User().Delete(intIDSys, 1);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult DeleteList(int[] lstIDSys)
        {
            ResultMessage result = new ResultMessage();
            result = new DAL_User().Delete(lstIDSys.ToList(), 1);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
